package com.winforsys.yellowbook;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MyListAdapter extends BaseAdapter {
	
	public enum ViewMode {VM_NORMAL, VM_REDIRECT, VM_DELETE };
	
	Context maincon;
	LayoutInflater inflater;
	ArrayList<MyListItem> source;
	int layout;
	
	ViewMode mode = ViewMode.VM_REDIRECT;
	
	AdapterView.OnItemClickListener listener;
	
	boolean transparent = false;
	
	public MyListAdapter(Context context, int layout, ArrayList<MyListItem> source) {
		this.maincon = context;
		this.layout = layout;
		this.source = source;
		this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public void setTransparent (boolean transparent) {
		this.transparent = transparent;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return source.size();
	}

	@Override
	public Object getItem(int pos) {
		// TODO Auto-generated method stub
		return source.get(pos);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		final AdapterView<?> parentView = (AdapterView<?>) parent; 
		final View childView = convertView;
		final int pos = position;
		
		if (convertView == null) {
			convertView = inflater.inflate(layout, parent, false);
		}
		
		ImageView img = (ImageView)convertView.findViewById(R.id.img);
		img.setImageResource(source.get(position).image);
		TextView name = (TextView)convertView.findViewById(R.id.name);
		name.setText(source.get(position).text);
		TextView phone = (TextView)convertView.findViewById(R.id.phone);
		phone.setText(source.get(position).desc);
		
		ImageView btn = (ImageView)convertView.findViewById(R.id.go);
		
		if (mode == ViewMode.VM_DELETE) {
			btn.setVisibility(View.VISIBLE);
			btn.setImageResource(R.drawable.delete);
		} else if (mode == ViewMode.VM_NORMAL) {
			btn.setVisibility(View.INVISIBLE);
			btn.setImageBitmap(null);
		}
		else {
			btn.setVisibility(View.VISIBLE);
			btn.setImageResource(R.drawable.forward);
		}
		
		
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (listener != null) {
					listener.onItemClick(parentView, childView, pos, R.id.go);
				}
			}
		});
		
		if (this.transparent == false) {
			RelativeLayout background = (RelativeLayout)convertView.findViewById(R.id.backgroud);
		
			background.setBackgroundColor((position % 2 == 0) ? 0x10FF0000 : 0x100000FF);
		}
		
		return convertView;
	}

	public void setOnItemButtonClickEventListenser(AdapterView.OnItemClickListener listener) {
		this.listener = listener;
	}
	
	public void setViewMode(ViewMode mode) {
		this.mode = mode;
	}
	
	public ViewMode getViewMode() {
		return mode;
	}
}
